import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:jxchmz/API/%E9%94%80%E5%94%AE%E7%AE%A1%E7%90%86/vipManage.dart';
import 'package:jxchmz/Getx%E5%B7%A5%E5%85%B7/GetxTools.dart';
import 'package:jxchmz/Getx%E5%B7%A5%E5%85%B7/KSProgressDialog.dart';

class VipUpdate extends StatefulWidget {
  @override
  _VipUpdateState createState() => new _VipUpdateState();
}

class _VipUpdateState extends State<VipUpdate> {
  var arguments = Get.arguments['Vip'];

  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  String phoneNum="";
  var _groupValue = "";
  //姓名
  TextEditingController NameController = new TextEditingController();
  //手机号
  TextEditingController PhoneController = new TextEditingController();
  //身份证号
  TextEditingController IdCardController = new TextEditingController();
  //会员卡号
  TextEditingController VipNumController = new TextEditingController();
  //开卡日期
  TextEditingController OpenIdCardDateController = new TextEditingController();
  //会员id
  int id=0;

//页面加载完成时
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    print("传递的数据$arguments");
    OpenIdCardDateController = new TextEditingController(text: arguments['openCardDate']);
    _groupValue = arguments['sex'].toString();
    NameController = new TextEditingController(text: arguments['name']);
    PhoneController = new TextEditingController(text: arguments['phone']);
    IdCardController = new TextEditingController(text: arguments['idCard']);
    VipNumController = new TextEditingController(text: arguments['vipCardNumber']);
    id = arguments['id'];
    setState(() {
    });
  }

  ///页面卸载
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  ///当 State 被暂时从视图树中移除时，会调用这个方法，同时页面切换时，也会调用。
  @override
  void deactivate() {
    // TODO: implement dispose
    super.deactivate();
  }

  ///在 initState() 之后调用，当 State 对象的依赖关系发生变化时，该方法被调用，初始化时也会调用。
  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
  }

  // TODO: add state variables and methods
  @override
  Widget build(BuildContext context) {
    // TODO: add widget build method
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Colors.white,
        title: Text("MemberModificationInformation".tr, style: TextStyle(fontSize: 15, color: Colors.black),),
        centerTitle: true,
        elevation: 0,
        iconTheme: IconThemeData(
            color: Colors.black
        ),
      ),
      body: SingleChildScrollView(
        padding: EdgeInsets.only(left: 15,right: 15),
        child: Column(
          children: <Widget>[
            Form(
              key: _formKey,
              child: Column(
                children: <Widget>[
                  TextFormField(
                    decoration: InputDecoration(
                      hintText: 'name'.tr,
                      hintStyle: TextStyle(fontSize: 13,color: Colors.black38),
                      prefixIcon: Icon(Icons.perm_identity),
                      enabledBorder: UnderlineInputBorder(
                        borderSide: BorderSide(
                            color: Colors.black12),
                      ),
                    ),
                    controller: NameController,
                    validator: (value) {
                      if (value!.isEmpty) {
                        return 'Pleaseenteryourname'.tr;
                      }
                      return null;
                    },
                  ),
                  TextFormField(
                    controller: PhoneController,
                    decoration: InputDecoration(
                      hintText: 'Mobilephonenumber'.tr,
                      hintStyle: TextStyle(fontSize: 13,color: Colors.black38),
                      prefixIcon: Icon(Icons.phone_android),
                      enabledBorder: UnderlineInputBorder(
                        borderSide: BorderSide(
                            color: Colors.black12),
                      ),
                    ),
                    validator: (value) {
                      RegExp reg = new RegExp(r'^\d{11}$');
                      if (!reg.hasMatch(value!)) {
                        return 'Pleaseenteran11-digitmobilephonenumber'.tr;
                      }
                      return null;
                    },
                  ),
                  Container(
                    padding: EdgeInsets.all(15),
                    alignment: Alignment.centerLeft,

                    child: Text("gender".tr),
                  ),
                  Row(
                    children: [
                      Row(
                        children: [
                          Radio(
                              value: "1",
                              activeColor: Colors.blue,
                              groupValue: _groupValue,
                              onChanged: (value) {
                                setState(() {
                                  _groupValue = value.toString();
                                });
                              }),
                          Text("man".tr)
                        ],
                      ),
                      Row(
                        children: [
                          Radio(
                              value: "2",
                              activeColor: Colors.blue,
                              groupValue: _groupValue,
                              onChanged: (value) {
                                setState(() {
                                  _groupValue = value.toString();
                                });
                              }),
                          Text("woman".tr)
                        ],
                      ),
                      Row(
                        children: [
                          Radio(
                              value: "3",
                              activeColor: Colors.blue,
                              groupValue: _groupValue,
                              onChanged: (value) {
                                setState(() {
                                  _groupValue = value.toString();
                                });
                              }),
                          Text("secrecy".tr)
                        ],
                      ),
                    ],
                  ),
                  TextFormField(
                    controller: IdCardController,
                    decoration: InputDecoration(
                      hintText: 'IDnumber'.tr,
                      hintStyle: TextStyle(fontSize: 13,color: Colors.black38),
                      prefixIcon: Icon(Icons.account_box_rounded),
                      enabledBorder: UnderlineInputBorder(
                        borderSide: BorderSide(
                            color: Colors.black12),
                      ),
                    ),
                  ),
                  TextFormField(
                    controller: VipNumController,
                    decoration: InputDecoration(
                      hintText: 'Membershipcardnumber'.tr,
                      hintStyle: TextStyle(fontSize: 13,color: Colors.black38),
                      prefixIcon: Icon(Icons.credit_card_rounded),
                      enabledBorder: UnderlineInputBorder(
                        borderSide: BorderSide(
                            color: Colors.black12),
                      ),
                    ),
                    validator: (value) {
                      if (value!.isEmpty) {
                        return 'Pleaseenteryourmembershipcardnumber'.tr;
                      }
                      return null;
                    },
                  ),
                  TextFormField(
                    controller: OpenIdCardDateController,
                    enabled: false,
                    decoration: InputDecoration(
                      hintText: 'Cardopeningdate'.tr,
                      hintStyle: TextStyle(fontSize: 13,color: Colors.black38),
                      prefixIcon: Icon(Icons.date_range),
                      enabledBorder: UnderlineInputBorder(
                        borderSide: BorderSide(
                            color: Colors.black12),
                      ),
                    ),
                  ),
                ],
              ),
            ),


          ],
        ),
      ),
      bottomNavigationBar: Container(
        padding: EdgeInsets.all(15),
        child: RaisedButton(
          child: Text('Submitinformation'.tr,style: TextStyle(color: Colors.white),),
          onPressed: () {
            //一步校验全部表单
            if (_formKey.currentState!.validate()) {
              //提交信息
              UploadVip();
            }
          },
          color: Colors.blue,
        ),
      ),
    );
  }
  UploadVip() async {
    //取出本地个人信息
    GetStorage getStorage = new GetStorage();
    var Userid = json.decode(getStorage.read("User"))['user']['userId'];
    //搜索参数
    var data = {
      "id":id,
      "name": NameController.text,
      "sex": _groupValue,
      "phone": PhoneController.text,
      "idCard":IdCardController.text,
      "vipCardNumber": VipNumController.text,
      "openCardDate": OpenIdCardDateController.text,
      "operator": Userid,
    };
    KSProgressDialog().LoadingToast("Undermodification".tr, context);
    var ResponseData = await VipManage().UpdateVip(data);
    KSProgressDialog().CloseLoading();
    if (ResponseData['code'] == 200) {
      GetxTools().SuccessToast("Themodificationwassuccessful".tr);
    } else {
      new GetxTools().ErrorToast("Modificationfailed".tr);
    }
    setState(() {});
  }
}
